﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Rules.cs" company="">
//   
// </copyright>
// <summary>
//   The rules.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Linq;

namespace Systemathics.FrameWork
{
    [Serializable]
    public sealed class Rules
    {
        private readonly Guid InstrumentId;
        private bool isbuyAllowed;
        private bool ishardToBorrow;
        private bool issellAllowed;
        private bool isshortable;
        private bool istradable;
        public long MaxNominalOrder;
        public decimal Tick;
        public double TickValue;

        public Rules(Guid id, double? tick, double? tickvalue, bool? isbuyallowed, bool? issellallowed, bool? isshortable, bool? ishardtoborrow, long maxnominal)
        {
            InstrumentId = id;
            Tick = new decimal((double) tick);
            TickValue = (double) tickvalue;
            isbuyAllowed = (bool) isbuyallowed;
            issellAllowed = (bool) issellallowed;
            this.isshortable = (bool) isshortable;
            ishardToBorrow = (bool) ishardtoborrow;
            MaxNominalOrder = maxnominal;
        }


        public void CheckTicksizeRule(ref Order o , MidpointRounding roundType)
        {
            // A creuser pour les regles par exchanges ...
            if(Tick == 0)
            {
                if (o.StaticData.Exchange.Name == "Chi-X")
                    Tick = o.TargetPrice < 10 ? .001m : .005m;
                if (o.StaticData.Exchange.Name == "TURQUOISE")
                    Tick = o.TargetPrice < 0.25m ? .001m : .01m;
                if (o.StaticData.Exchange.Name == "Stugttart Stock Exchange")
                    Tick = o.TargetPrice < 1m ? .001m : .01m;
                else
                    Tick = .01m;

            }

            var reminder = o.TargetPrice % Tick;
            if (reminder != 0)
            {
                var result = o.TargetPrice - reminder;
                switch (roundType)
                {
                    case MidpointRounding.ToEven:
                        o.TargetPrice = result - Tick;
                        break;
                    case MidpointRounding.AwayFromZero:
                        o.TargetPrice = result + Tick;
                        break;
                }
            }
        }

        public bool isBuyAllowed
        {
            get { return isbuyAllowed; }
            set
            {
                isbuyAllowed = value;
                using (var db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString()))
                {
                    DBRules rule = db.DBRules.Where(u => u.DBInstruments.ID == InstrumentId).FirstOrDefault();
                    rule.isBuyAllowed = isbuyAllowed;
                    db.SaveChanges(true);
                }
            }
        }
        public bool isSellAllowed
        {
            get { return issellAllowed; }
            set
            {
                issellAllowed = value;
                using (var db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString()))
                {
                    DBRules rule = db.DBRules.Where(u => u.DBInstruments.ID == InstrumentId).FirstOrDefault();
                    rule.isSellAllowed = issellAllowed;
                    db.SaveChanges(true);
                }

                if (!issellAllowed)
                    isShortable = false;
            }
        }
        public bool isShortable
        {
            get { return isshortable; }
            set
            {
                isshortable = value;
                using (var db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString()))
                {
                    DBRules rule = db.DBRules.Where(u => u.DBInstruments.ID == InstrumentId).FirstOrDefault();
                    rule.isShortable = isshortable;
                    db.SaveChanges(true);
                }
            }
        }
        public bool isHardToBorrow
        {
            get { return ishardToBorrow; }
            set
            {
                ishardToBorrow = value;
                using (var db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString()))
                {
                    DBRules rule = db.DBRules.Where(u => u.DBInstruments.ID == InstrumentId).FirstOrDefault();
                    rule.isHardToBorrow = ishardToBorrow;
                    db.SaveChanges(true);
                }
            }
        }
        public bool isTradable
        {
            get
            {
                istradable = isBuyAllowed & isSellAllowed & isShortable & !isHardToBorrow;
                return istradable;
            }

            set { istradable = value; }
        }
    }
} 